<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../layui/css/layui.css" media="all">
    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
</head>
<body>

<form class="layui-form" action="" lay-filter="orderForm" >
    <div class="layui-form-item" style="margin-top:10px">
        <div class="layui-inline">
            <label class="layui-form-label">产品类别：</label>
            <div class="layui-input-block">
<!--                <select class="layui-form-select" id="partName" name="partName" lay-filter="partFilter">-->
<!--                </select>-->
                <select class="layui-form-select" name="city" lay-verify="required">
                    <option value=""></option>
                    <option value="0">北京</option>
                    <option value="1">上海</option>
                    <option value="2">广州</option>
                    <option value="3">深圳</option>
                    <option value="4">杭州</option>
                </select>
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">产品名称：</label>
            <div class="layui-input-block">
                <select class="layui-form-select" name="city" lay-verify="required">
                    <option value=""></option>
                    <option value="0">北京</option>
                    <option value="1">上海</option>
                    <option value="2">广州</option>
                    <option value="3">深圳</option>
                    <option value="4">杭州</option>
                </select>
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">产地：</label>
            <div class="layui-input-block">
                <select class="layui-form-select" name="city" lay-verify="required">
                    <option value=""></option>
                    <option value="0">北京</option>
                    <option value="1">上海</option>
                    <option value="2">广州</option>
                    <option value="3">深圳</option>
                    <option value="4">杭州</option>
                </select>
            </div>
        </div>
        <button type="button" id="formSubmit" class="layui-inline layui-btn" lay-submit="" lay-filter="query">查询</button>
    </div>
</form>

<table class="layui-hide" id="test" lay-filter="test"></table>
<style type="text/css">
    .layui-form-select {
        width: 150px;
    }
    .layui-form-label {
        width: 70px;
    }
</style>
<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="getCheckData">获取选中行数据</button>
        <button class="layui-btn layui-btn-sm" lay-event="getCheckLength">获取选中数目</button>
        <button class="layui-btn layui-btn-sm" lay-event="isAll">验证是否全选</button>
    </div>
</script>

<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">入库</a>

</script>
<script type="text/html" id="barDemo1">
    <a href="./outlist.html" class="layui-btn layui-btn-xs">出入库查询</a>
</script>

<script src="https://www.layuicdn.com/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script src="https://cdn.bootcss.com/axios/0.19.0-beta.1/axios.min.js"></script>

<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->

<script>
    layui.use(['table','form','jquery','layer'], function(){
        var table = layui.table;
        var form = layui.form;
        var $ = layui.jquery;
        var layer = layui.layer;

        table.render({
            id : 'departmentTable'
            ,elem: '#test'
            ,url:'/firstWeb/inproduct/queryAll'
            ,title: '出入库管理表'
            ,where: {}
            ,cols: [[
                {field:'id', title:'编号'}
                ,{field:'sort', title:'产品类别', templet: function (data) {
                            return data.sort.name;
                        }}
                ,{field:'time',title:'产品名', templet: function (data) {
                        return data.product.name;
                    }}
                ,{field:'product',title:'产地', templet: function (data) {
                        return data.product.area;
                    }}
                ,{field:'p_area',title:'规格', templet: function (data) {
                        return data.product.ml;
                    }}
                ,{fixed: 'right', title:'出入库查询', toolbar: '#barDemo1'}
                ,{field:'count',title:'库存数量'}
                ,{fixed: 'right', title:'入库', toolbar: '#barDemo'}
                /*
                 {field: 'o_id', title: '编号'}
                , {
                    field: 'product', title: '产品名', templet: function (data) {
                        return data.product.name;
                    }
                }
                , {
                    field: 'client', title: '入库申请人', templet: function (data) {
                        return data.client.name;
                    }
                }
                , {field: 'o_remark', title: '入库备注'}
                , {field: 'o_int', title: '入库数量'}
                , {field: 'o_time', title: '入库时间'}
                 */
            ]]
            ,page: true // 默认一页十条
            ,limit:5
            ,limits: [1,5,10,20]
        });

        function tableReload() {
            var data = form.val('departmentForm');
            table.reload('departmentTable',{
                where : data
                ,page: {
                    curr: 1
                }
            })
        }
        $("#formSubmit").click(function() {
            tableReload();
        });

        $("#add").click(function() {
            layer.open({
                type: 2 //此处以iframe举例
                ,title: '新增页面'
                ,area: ['390px', '260px']
                ,shade: 0
                ,maxmin: true
                ,content: 'dialog.html'
                ,btn: ['提交', '关闭']
                ,yes: function(index){
                    var data = window['layui-layer-iframe' + index].getValue();
                    if (!data.name) {
                        layer.msg("部门名称不能为空");
                        return false;
                    }
                    //发送新增请求 get/post √
                    axios.post('/firstWeb/part/savePart', data).then(function (response) {
                        //post请求执行成功的回调
                        if (response.data.status == "1") {
                            layer.close(index);
                            //刷新表格
                            tableReload();
                            layer.msg("新增成功");
                        } else {
                            layer.msg(response.data.msg)
                        }
                    }).catch(function (error) {
                        console.log(error);
                    });
                }
                ,no: function(index){//5
                    layer.close(index);
                }
            });
        })



        //监听行工具事件
        table.on('tool(test)', function(obj){
            var data = obj.data;
            console.log(obj);
            if(obj.event === 'del'){//删除
                layer.confirm('真的删除行么', function(index){
                    axios.post('/firstWeb/part/updateState', data).then(function (response) {
                        //post请求执行成功的回调
                        if (response.data.status == "1") {
                            layer.close(index);
                            //刷新表格
                            tableReload();
                            layer.msg("删除成功");
                        } else {
                            layer.msg(response.data.msg)
                        }
                    }).catch(function (error) {
                        console.log(error);
                    });
                    obj.del();
                    layer.close(index);
                });
            } else if(obj.event === 'edit') {//编辑
                layer.open({
                    type: 2 //此处以iframe举例
                    ,title: '商品入库'
                    ,area: ['600px', '360px']
                    ,shade: 0
                    ,maxmin: true
                    ,content: 'addproduct.html'
                    ,btn: ['提交', '关闭']
                    ,yes: function(index){
                        var data = window['layui-layer-iframe' + index].getValue();
                        data.id = obj.data.id;
                        if (!data.name) {
                            layer.msg("部门名称不能为空");
                            return false;
                        }
                        //发送新增请求 get/post √
                        axios.post('/firstWeb/part/update', data).then(function(response) {
                            //post请求执行成功的回调
                            if (response.data.status == "1") {
                                layer.close(index);
                                //刷新表格
                                tableReload();
                                layer.msg("更新成功");
                            } else {
                                layer.msg(response.data.msg)
                            }
                        }).catch(function (error) {
                            console.log(error);
                        });
                    }
                    ,no: function(index){//5
                        layer.close(index);
                    }
                });
            }
        });
    });
</script>

</body>
</html>
